package com.ruoyi.util.freemarkercreatepdf;

import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringWriter;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import com.itextpdf.text.Document;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfWriter;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class Generator {
	
	@Autowired
	private PdfConfig pdfConfig;

	/**
	 * @Description: 功能描述 生成HTML
	 * @param: 参数描述
	 * @param templaten
	 * @param variables
	 * @return
	 * @throws Exception
	 * @throws: 异常描述
	 *
	 * @version: v1.0.3
	 * @author: huangd
	 * @date: 2022年8月31日 上午10:48:10
	 *
	 *        Modification History: Date Author Version Description
	 *        ---------------------------------------------------------* 2022年8月31日
	 *        huangd v1.0.3.1 创建
	 */
	public static String htmlGenerate(String templateName, String tempaltePath, Map<String, Object> variables) {
		Configuration config = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
		BufferedWriter writer = null;
		String htmlStr = "";
		try {
			Resource resource = new ClassPathResource(tempaltePath);
			File sourceFile = resource.getFile();
			config.setDirectoryForTemplateLoading(sourceFile);
			Template tp = config.getTemplate(templateName);
			StringWriter stringWriter = new StringWriter();
			writer = new BufferedWriter(stringWriter);
			tp.process(variables, writer);
			htmlStr = stringWriter.toString();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TemplateException e) {
			e.printStackTrace();
		} finally {
			if (writer != null) {
				try {
					writer.flush();
					writer.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return htmlStr;
	}

	public void pdfGeneratePlus(String templateName, String tempaltePath, Map<String, Object> dataMap,
			String targetPdf, Rectangle pageSize, String header, boolean isFooter, File watermark) throws Exception {
		/**
		 * 根据freemarker模板生成html
		 */
		//String htmlStr = htmlGenerate(templateName, tempaltePath, dataMap);
		//final String charsetName = "UTF-8";
		OutputStream out = new ByteArrayOutputStream();
		Document document = new Document(pageSize);
		/**
		 *	 设置边距
		 */
		// document.setMargins(30, 30, 30, 30);
		PdfWriter writer = PdfWriter.getInstance(document, out);
       /**
        *	添加水印和页码
        */
      if (watermark  == null) {
    	  //  watermark = new File ("D:/test123456/1234567.jpg");
    	  Resource resource = new ClassPathResource(pdfConfig.getWatermarkPath());
    	  watermark = resource.getFile();
      }
      PDFBuilder builder = new PDFBuilder(header, 10, pageSize, watermark, isFooter);
      writer.setPageEvent(builder);
      document.open();
	}

//	public static void main(String[] args) {
//		Map<String, Object> dataMap = new HashMap<String, Object>();
//		List<String> titleList = Arrays.asList("属性1", "属性2", "属性3");
//		dataMap.put("titleList", titleList);
//		List<List<String>> dataList = new ArrayList<List<String>>();
//		for (int i = 0; i < 100; i++) {
//			dataList.add(Arrays.asList("数据1_" + i, "数据2_" + i, "数据3_" + i));
//		}
//		dataMap.put("dataList", dataList);
//		try {
//			String str = Generator.htmlGenerate("123.ftl", "templates", dataMap);
//			System.out.println("11111111111......." + str);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}

}
